﻿using System;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x020000D1 RID: 209
	[RequireComponent(typeof(Animator))]
	public class VRAnimatorController : MonoBehaviour
	{
		// Token: 0x1700008E RID: 142
		// (get) Token: 0x06000624 RID: 1572 RVA: 0x00005F2E File Offset: 0x0000412E
		// (set) Token: 0x06000625 RID: 1573 RVA: 0x00005F36 File Offset: 0x00004136
		public Vector3 velocity { get; set; }

		// Token: 0x06000626 RID: 1574 RVA: 0x00038B64 File Offset: 0x00036D64
		public void Start()
		{
			this.animator = base.GetComponent<Animator>();
			this.fixFeet = base.gameObject.AddComponent<FixFeet>();
			this.animator.SetBool("IsStrafing", true);
			this.playerLastPosition = this.characterController.position;
			this.cameraPivot = new GameObject().transform;
			this.cameraPivot.name = "Camera Pivot";
			this.cameraPivot.position = this.characterController.position + this.characterController.rotation * this.cameraOffset;
			this.cameraPivot.rotation = this.characterController.rotation;
			this.cameraPivot.parent = this.characterController;
			this.cam.parent = this.cameraPivot;
		}

		// Token: 0x06000627 RID: 1575 RVA: 0x00038C3C File Offset: 0x00036E3C
		public void Update()
		{
			if (!this.oculusSetup.isFinished)
			{
				if (this.fixFeet != null)
				{
					this.fixFeet.weight = 1f;
				}
				this.velocity = Vector3.zero;
				this.animator.SetFloat("Right", 0f);
				this.animator.SetFloat("Forward", 0f);
				return;
			}
			this.RotateCharacter(this.cam.forward, this.maxViewAngle, this.cameraPivot);
			Vector3 velocityTarget = this.GetVelocityTarget();
			this.velocity = Vector3.MoveTowards(this.velocity, velocityTarget, Time.deltaTime * this.linearAcceleration);
			this.velocity = Vector3.SmoothDamp(this.velocity, velocityTarget, ref this.velocityC, this.smoothAccelerationTime);
			base.transform.position = new Vector3(this.characterController.position.x, base.transform.position.y, this.characterController.position.z);
			if (this.fixFeet != null)
			{
				float target = (!(this.velocity == Vector3.zero)) ? 0f : 1f;
				this.fixFeet.weight = Mathf.MoveTowards(this.fixFeet.weight, target, Time.deltaTime * 3f);
			}
			this.animator.SetFloat("Right", this.velocity.x);
			this.animator.SetFloat("Forward", this.velocity.z);
		}

		// Token: 0x06000628 RID: 1576 RVA: 0x00038DF8 File Offset: 0x00036FF8
		public Vector3 GetVelocityTarget()
		{
			Vector3 zero = Vector3.zero;
			this.playerVelocity = (this.characterController.position - this.playerLastPosition) / Time.deltaTime;
			this.playerLastPosition = this.characterController.position;
			return zero + Quaternion.Inverse(base.transform.rotation) * this.playerVelocity * this.locomotionSpeed;
		}

		// Token: 0x06000629 RID: 1577 RVA: 0x00038E70 File Offset: 0x00037070
		public void RotateCharacter(Vector3 forward, float maxAngle, Transform fix = null)
		{
			if (maxAngle >= 180f)
			{
				return;
			}
			Quaternion rotation = (!(fix != null)) ? Quaternion.identity : fix.rotation;
			if (maxAngle <= 0f)
			{
				this.characterController.rotation = Quaternion.LookRotation(new Vector3(forward.x, 0f, forward.z));
				if (fix != null)
				{
					fix.rotation = rotation;
				}
				return;
			}
			Vector3 vector = this.characterController.InverseTransformDirection(forward);
			float num = Mathf.Atan2(vector.x, vector.z) * 57.29578f;
			if (Mathf.Abs(num) > Mathf.Abs(maxAngle))
			{
				float angle = num - maxAngle;
				if (num < 0f)
				{
					angle = num + maxAngle;
				}
				this.characterController.rotation = Quaternion.AngleAxis(angle, this.characterController.up) * this.characterController.rotation;
			}
			if (fix != null)
			{
				fix.rotation = rotation;
			}
		}

		// Token: 0x04000549 RID: 1353
		[Header("Component References")]
		public VRSetup oculusSetup;

		// Token: 0x0400054A RID: 1354
		public Transform characterController;

		// Token: 0x0400054B RID: 1355
		public Transform cam;

		// Token: 0x0400054C RID: 1356
		[Tooltip("Offset of the VR camera")]
		[Header("Main Properties")]
		public Vector3 cameraOffset;

		// Token: 0x0400054D RID: 1357
		[Tooltip("How long to accelerate to target velocity using SmoothDamp?")]
		public float smoothAccelerationTime = 0.2f;

		// Token: 0x0400054E RID: 1358
		[Tooltip("How fast to accelerate liearily? If this is zero, will only use smooth acceleration.")]
		public float linearAcceleration = 2f;

		// Token: 0x0400054F RID: 1359
		[Tooltip("Rotate the character along if camera is looking too far left/right.")]
		public float maxViewAngle = 60f;

		// Token: 0x04000550 RID: 1360
		[Tooltip("The master speed of locomotion animations.")]
		public float locomotionSpeed = 1f;

		// Token: 0x04000551 RID: 1361
		public Animator animator;

		// Token: 0x04000552 RID: 1362
		public Vector3 velocityC;

		// Token: 0x04000553 RID: 1363
		public bool rootCorrection;

		// Token: 0x04000554 RID: 1364
		public Vector3 playerVelocity;

		// Token: 0x04000555 RID: 1365
		public Vector3 playerLastPosition;

		// Token: 0x04000556 RID: 1366
		public FixFeet fixFeet;

		// Token: 0x04000557 RID: 1367
		public Transform cameraPivot;
	}
}
